package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.InspectionTaskDTO;
import com.neusoft.neuiotms.dcom.model.InspectionTask;
import com.neusoft.neuiotms.dcom.model.InspectionTaskProject;
import com.neusoft.neuiotms.dcom.model.enums.InspectionTaskStatus;
import com.neusoft.neuiotms.dcom.repository.InspectionTaskRepository;

@Component
public class InspectionTaskConverter extends AbstractConverter<InspectionTask, InspectionTaskDTO> {

    @Autowired
    private InspectionTaskRepository inspectionTaskRepository;

    @Autowired
    private InspectionTaskProjectConverter inspectionTaskProjectConverter;

    @Override
    public InspectionTask toModel(final InspectionTaskDTO dto) {
        InspectionTask model;
        if (dto.isNew()) {
            model = new InspectionTask();
        } else {
            model = this.inspectionTaskRepository.getOne(dto.getId());
        }
        model.setEndTaskTime(dto.getEndTaskTime());
        model.setFinishTaskTime(dto.getFinishTaskTime());
        model.setGetTaskTime(dto.getGetTaskTime());
        model.setName(dto.getName());
        model.setNote(dto.getNote());
        model.setStartTaskTime(dto.getStartTaskTime());
        model.setUsercode(dto.getUsercode());
        model.setUsername(dto.getUsername());
        model.setClaimCode(dto.getClaimCode());
        model.setClaimName(dto.getClaimName());
        model.setStatus(StringUtils.isEmpty(dto.getStatus()) ? InspectionTaskStatus.notStarted
                : InspectionTaskStatus.valueOf(dto.getStatus()));

        if ((dto.getInspectionTaskProject() != null) && !dto.getInspectionTaskProject().isEmpty()) {
            final List<InspectionTaskProject> projectList =
                    this.inspectionTaskProjectConverter.toListModel(dto.getInspectionTaskProject());
            Integer normal = 0;
            Integer alarm = 0;
            Integer earlyWarning = 0;
            Integer uninspected = 0;
            for (final InspectionTaskProject p : projectList) {
                normal += p.getNormal();
                alarm += p.getAlarm();
                earlyWarning += p.getEarlyWarning();
                uninspected += p.getUninspected();
            }
            model.setNormal(normal);
            model.setAlarm(alarm);
            model.setEarlyWarning(earlyWarning);
            model.setUninspected(uninspected);
            model.setInspectionTaskProject(projectList);
        }

        return model;
    }

    @Override
    public InspectionTaskDTO toDTO(final InspectionTask model, final boolean forListView) {
        final InspectionTaskDTO dto = new InspectionTaskDTO();
        dto.setAlarm(model.getAlarm());
        dto.setEarlyWarning(model.getEarlyWarning());
        dto.setUninspected(model.getUninspected());
        dto.setNormal(model.getNormal());
        dto.setCreateTime(model.getCreateTime());
        dto.setEndTaskTime(model.getEndTaskTime());
        dto.setFinishTaskTime(model.getFinishTaskTime());
        dto.setGetTaskTime(model.getGetTaskTime());
        dto.setId(model.getId());
        dto.setNote(model.getNote());
        dto.setStartTaskTime(model.getStartTaskTime());
        dto.setStatus(model.getStatus().name());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setUsercode(model.getUsercode());
        dto.setUsername(model.getUsername());
        dto.setClaimCode(model.getClaimCode());
        dto.setClaimName(model.getClaimName());
        dto.setName(model.getName());
        dto.setInspectionType(model.getInspectionRule().getInspectionType());
        if (!forListView) {
            if ((model.getInspectionTaskProject() != null) && !model.getInspectionTaskProject().isEmpty()) {
                dto.setInspectionTaskProject(
                        model.getInspectionTaskProject().stream().map(m -> {
                            return this.inspectionTaskProjectConverter.toDTO(m, forListView);
                        }).collect(Collectors.toList()));
            }

        }

        return dto;
    }

}
